package builder_pattern;

/**
 * 建造者模式
 */
public class builder {

  public static void main(String[] args) {
    FoodStore foodStore = new FoodStore();
    Meal meal1 = foodStore.createFood(new Breakfast());
    Meal meal2 = foodStore.createFood(new Lunch());
    System.out.println("小明早上吃的是:"+meal1.getFood()+",喝的饮料是:"+meal1.getDrinks());
    System.out.println("小明中午吃的是:"+meal2.getFood()+",喝的饮料是:"+meal2.getDrinks());
  }
}

/**
 * 食物
 * Product：示被构造的复杂对象。
 */
class Meal{
  private String food;
  private String drinks;

  public String getFood() {
    return food;
  }

  public void setFood(String food) {
    this.food = food;
  }

  public String getDrinks() {
    return drinks;
  }

  public void setDrinks(String drinks) {
    this.drinks = drinks;
  }
}

/**
 * 食物的标准接口
 * Builder：指定一个抽象的接口，规定该产品所需实现部件的创建，并不涉及具体的对象部件的创建。
 */
interface IBuilderFood{
  void buildFood();
  void buildDrinks();
  Meal createMeal();
}

/**
 * 早餐
 * ConcreteBuilder：需实现Builder接口，并且针对不同的逻辑，进行不同方法的创建，最终提供该产品的实例。
 */
class Breakfast implements IBuilderFood{
  Meal meal;

  public Breakfast() {
    this.meal = new Meal();
  }

  public void buildFood() {
    meal.setFood("煎饼");
  }

  public void buildDrinks() {
    meal.setDrinks("豆浆");
  }

  public Meal createMeal() {
    return meal;
  }
}

/**
 * 午餐
 * ConcreteBuilder：需实现Builder接口，并且针对不同的逻辑，进行不同方法的创建，最终提供该产品的实例。
 */
class Lunch implements IBuilderFood{
  Meal meal;

  public Lunch() {
    this.meal = new Meal();
  }

  public void buildFood() {
    meal.setFood("盒饭");
  }

  public void buildDrinks() {
    meal.setDrinks("果汁");
  }

  public Meal createMeal() {
    return meal;
  }
}


/**
 * Director：用来创建复杂对象的部分，对该部分进行完整的创建或者按照一定的规则进行创建。
 */
class FoodStore{
  public Meal createFood(IBuilderFood builderFood){
    builderFood.buildDrinks();
    builderFood.buildFood();
    return builderFood.createMeal();
  }
}


